/*
 * Copyright (c) 2010-2011 NutShell.
 * [Project:YAM,Id:Role.java  2011-10-03 17:52:07 poplar.mumu ]
 */
package org.mumu.pawn.domain;

import org.hibernate.validator.constraints.Length;
import org.mumu.pawn.fm.annotation.OrderByColumn;
import org.richfaces.component.SortOrder;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * 角色信息表 实体
 * <br/>
 *
 * @author poplar.mumu
 * @version 1.0 2011-10-03 17:52:07
 * @since JDK 1.0
 */
@Entity
@Table(name = "ROLE")
public class Role implements Serializable {
    private static final long serialVersionUID = -6990583450195613699L;

    /**
     * 主键
     */
    private long id;
    /**
     * 角色名称
     */
    @OrderByColumn
    private String roleName;
    /**
     * 角色标识
     */
    private String roleSign;
    /**
     * 角色描述
     */
    private String roleDesc;
    /**
     * 创建时间
     */
    @OrderByColumn(order = SortOrder.ascending)
    private long createTime;
    /**
     * 创建人
     */
    private User createActor;
    /**
     * 修改时间
     */
    @OrderByColumn
    private long updateTime;
    /**
     * 修改人
     */
    private long updateActor;
    /**
     * 排序
     */
    private int sort;

    /**
     * 该角色下的所有用户信息
     */
    private Set<User> users;
    /**
     * 该角色下的所有权限组信息
     */
    private Set<Module> modules = new HashSet<Module>();

    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "ROLE_POWER", inverseJoinColumns = @JoinColumn(name = "MODULE"),
            joinColumns = @JoinColumn(name = "ROLE", unique = true))
    public Set<Module> getModules() {
        return modules;
    }

    public void setModules(Set<Module> modules) {
        this.modules = modules;
    }

    /**
     * @return 主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long getId() {
        return id;
    }

    /**
     * 设置 主键[ID]
     *
     * @param id 主键
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return 角色名称
     */
    @Column(name = "ROLE_NAME")
    @NotNull
    @Length(max = 30, min = 2)
    public String getRoleName() {
        return roleName;
    }

    /**
     * 设置 角色名称[ROLE_NAME]
     *
     * @param roleName 角色名称
     */
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    /**
     * @return 角色标识
     */
    @Column(name = "ROLE_SIGN")
    @NotNull
    @Length(max = 15, min = 2)
    public String getRoleSign() {
        return roleSign;
    }

    /**
     * 设置 角色标识[ROLE_SIGN]
     *
     * @param roleSign 角色标识
     */
    public void setRoleSign(String roleSign) {
        this.roleSign = roleSign;
    }

    /**
     * @return 角色描述
     */
    @Column(name = "ROLE_DESC")
    public String getRoleDesc() {
        return roleDesc;
    }

    /**
     * 设置 角色描述[ROLE_DESC]
     *
     * @param roleDesc 角色描述
     */
    public void setRoleDesc(String roleDesc) {
        this.roleDesc = roleDesc;
    }

    /**
     * @return 创建时间
     */
    @Column(name = "CREATE_TIME")
    public long getCreateTime() {
        return createTime;
    }

    /**
     * 设置 创建时间[CREATE_TIME]
     *
     * @param createTime 创建时间
     */
    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    /**
     * @return 创建人
     */
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "CREATE_ACTOR")
    public User getCreateActor() {
        return createActor;
    }

    /**
     * 设置 创建人[CREATE_ACTOR]
     *
     * @param createActor 创建人
     */
    public void setCreateActor(User createActor) {
        this.createActor = createActor;
    }

    /**
     * @return 修改时间
     */
    @Column(name = "UPDATE_TIME")
    public long getUpdateTime() {
        return updateTime;
    }

    /**
     * 设置 修改时间[UPDATE_TIME]
     *
     * @param updateTime 修改时间
     */
    public void setUpdateTime(long updateTime) {
        this.updateTime = updateTime;
    }

    /**
     * @return 修改人
     */
    @Column(name = "UPDATE_ACTOR")
    public long getUpdateActor() {
        return updateActor;
    }

    /**
     * 设置 修改人[UPDATE_ACTOR]
     *
     * @param updateActor 修改人
     */
    public void setUpdateActor(long updateActor) {
        this.updateActor = updateActor;
    }

    /**
     * @return 排序
     */
    public int getSort() {
        return sort;
    }

    /**
     * 设置 排序[SORT]
     *
     * @param sort 排序
     */
    public void setSort(int sort) {
        this.sort = sort;
    }

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
    @JoinTable(name = "USR_ROLE",
            joinColumns = @JoinColumn(name = "USR",unique = true,nullable = true),
            inverseJoinColumns = @JoinColumn(name = "ROLE",unique = true,nullable = true))
    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }
}